package com.ruoyi.system.controller;

import java.util.List;
import jakarta.servlet.http.HttpServletResponse;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import com.ruoyi.common.annotation.Log;
import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.enums.BusinessType;
import com.ruoyi.system.domain.aiChat;
import com.ruoyi.system.service.IaiChatService;
import com.ruoyi.common.utils.poi.ExcelUtil;
import com.ruoyi.common.core.page.TableDataInfo;
import org.springframework.web.bind.annotation.ResponseBody;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

/**
 * aiChatController
 * 
 * @author ruoyi
 * @date 2025-01-09
 */
@RestController
@RequestMapping("/system/aiChat")
public class aiChatController extends BaseController
{
    private static final Logger logger = LoggerFactory.getLogger(aiChatController.class);

    @Autowired
    private IaiChatService aiChatService;

    /**
     * 查询aiChat列表
     */
    @PreAuthorize("@ss.hasPermi('system:aiChat:list')")
    @GetMapping("/list")
    public TableDataInfo list(aiChat aiChat)
    {
        startPage();
        List<aiChat> list = aiChatService.selectaiChatList(aiChat);
        return getDataTable(list);
    }

    /**
     * 导出aiChat列表
     */
    @PreAuthorize("@ss.hasPermi('system:aiChat:export')")
    @Log(title = "aiChat", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    public void export(HttpServletResponse response, aiChat aiChat)
    {
        List<aiChat> list = aiChatService.selectaiChatList(aiChat);
        ExcelUtil<aiChat> util = new ExcelUtil<aiChat>(aiChat.class);
        util.exportExcel(response, list, "aiChat数据");
    }

    /**
     * 获取aiChat详细信息
     */
    @PreAuthorize("@ss.hasPermi('system:aiChat:query')")
    @GetMapping(value = "/{id}")
    public AjaxResult getInfo(@PathVariable("id") Long id)
    {
        return success(aiChatService.selectaiChatById(id));
    }

    /**
     * 新增aiChat
     */
    @PreAuthorize("@ss.hasPermi('system:aiChat:add')")
    @Log(title = "aiChat", businessType = BusinessType.INSERT)
    @PostMapping
    public AjaxResult add(@RequestBody aiChat aiChat)
    {
        return toAjax(aiChatService.insertaiChat(aiChat));
    }

    /**
     * 修改aiChat
     */
    @PreAuthorize("@ss.hasPermi('system:aiChat:edit')")
    @Log(title = "aiChat", businessType = BusinessType.UPDATE)
    @PutMapping
    public AjaxResult edit(@RequestBody aiChat aiChat)
    {
        return toAjax(aiChatService.updateaiChat(aiChat));
    }

    /**
     * 删除aiChat
     */
    @PreAuthorize("@ss.hasPermi('system:aiChat:remove')")
    @Log(title = "aiChat", businessType = BusinessType.DELETE)
	@DeleteMapping("/{ids}")
    public AjaxResult remove(@PathVariable Long[] ids)
    {
        return toAjax(aiChatService.deleteaiChatByIds(ids));
    }

    /**
     * 根据sessionId获取对话记录
     */
    @PreAuthorize("@ss.hasPermi('system:aiChat:query')")
    @GetMapping("/listBySessionId/{sessionId}")
    @ResponseBody
    public AjaxResult getDialogsBySessionId(@PathVariable("sessionId") Long sessionId)
    {
        aiChat chat = new aiChat();
        chat.setSessionId(sessionId);
        List<aiChat> list = aiChatService.selectaiChatList(chat);
        logger.info("获取会话ID：{}的对话记录，共{}条", sessionId, list.size());
        for (aiChat item : list) {
            logger.info("对话记录：sessionId={}, userName={}, dialog={}", 
                item.getSessionId(), item.getUserName(), item.getDialog());
        }
        return success(list);
    }
}
